package br.ufba.lasid.jitclouds.mda.queue;

import br.ufba.lasid.jitclouds.mda.utils.MQProperties;
import br.ufba.lasid.jitclouds.mda.vo.MDA;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TBase;
import org.apache.thrift.TSerializer;

public class Publisher {

    private static final String EXCHANGE_NAME = "vm_metric";
    private static final String EXCHANGE_TYPE = "topic";

    public static void main(String[] args) throws Exception {

        String ip = StringUtils.EMPTY;
        String port = StringUtils.EMPTY;
        String queue = StringUtils.EMPTY;
        boolean flagGood = false;
        // obtém uma conexão para acesso ao servidor AMQP
        ConnectionFactory factory = new ConnectionFactory();

        if (args != null && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                String string = args[i];

                if (StringUtils.equals("-ip", string) && args.length >= (i + 2)) {
                    ip = args[i + 1];
                } else if (StringUtils.equals("-port", string) && args.length >= (i + 2)) {
                    port = args[i + 1];
                } else if (StringUtils.equals("-queue", string) && args.length >= (i + 2)) {
                    queue = args[i + 1];
                }
            }
            if (StringUtils.isEmpty(ip) || StringUtils.isBlank(port) || StringUtils.isBlank(queue)) {
                System.out.println("You should type the parameter and the values:");
                System.out.println("-ip value -port value -queue value");
                System.out.println("-ip 192.168.188.9 -port 5672 -queue vm_metric");
                flagGood = false;
            } else {
                System.out.println("Reading parameters from shell [" + ip + ":" + port + "] at queue: " + queue);
                factory.setHost(ip);
                factory.setPort(Integer.parseInt(port));
                flagGood = true;
            }
        } else {
            System.out.println("Reading parameters from properties file [" + MQProperties.get(MQProperties.HOST) + ":" + MQProperties
                    .get(MQProperties.PORT) + "]");
            factory.setHost(MQProperties.get(MQProperties.HOST));
            factory.setPort(Integer.parseInt(MQProperties
                    .get(MQProperties.PORT)));
            queue = EXCHANGE_NAME;
            flagGood = true;
        }

        if (flagGood) {
            Connection connection = factory.newConnection();

            // obtém um canal, uma forma de multiplexar uma mesma conexão
            Channel channel = connection.createChannel();

            // channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);
            channel.queueDeclare(queue, false, false, false, null);

            // Create a new instance of the Thrift Serializer
            TSerializer serializer = new TSerializer();

            // Do this 10 times
            for (int i = 0; i < 10; i++) {

                MDA item = (MDA) getItem(i);
                // Serialize our domain object to a byte array
                byte[] payload = serializer.serialize(item);

                // Publish message on the queue using our domain
                // object as the message (or payload)
                channel.basicPublish("", queue, null, payload);

                // Print a friendly message
                System.out.println(" [x] Sent '" + item.toString() + "'");
                Thread.sleep(2000);
            }
            channel.close();
            connection.close();
        }
    }

    private static TBase getItem(int i) {
        // Create an instance of our Domain object.
        // Keep in mind this is a Thrift generated bean.
        Date date = new Date();
        MDA item = new MDA(String.valueOf(i), "192.168.188.9", date.getTime(), "cloud9", "on", EXCHANGE_NAME);
        return item;
    }
}
